﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Stone
{
    public partial class FormMain : Form
    {
        Module_Gps_Osm moduleGpsOsm = new Module_Gps_Osm();
        public GPSHandler GpsHandler;

        public FormMain()
        {
            InitializeComponent();

            GpsHandler = new GPSHandler(this);
            GpsHandler.NewGPSFix += new GPSHandler.NewGPSFixHandler(this.GPSEventHandler);
        }
        private void GPSEventHandler(object sender, GPSHandler.GPSEventArgs e)
        {
            addNMEAsentenceInListBox(e.Sentence);
            moduleGpsOsm.ReceiveNMEA(GpsHandler, e);
            moduleGpsOsm.PaintViewport_new(panelViewport);
        }
        private void panelViewport_Paint(object sender, PaintEventArgs e)
        {
            if (null != moduleGpsOsm.ImageToPaint)
                panelViewport.CreateGraphics().DrawImage(moduleGpsOsm.ImageToPaint, new Point(0, 0));
        }
        private void FormMain_Load(object sender, EventArgs e)
        {
            Settings.LoadSettings();

            GpsHandler.Start(Settings.GPS_ComPort, int.Parse(Settings.GPS_ComPort_Baudrate));
            //Thread.Sleep(3000);

            if (!GpsHandler.IsPortOpen)
            {
                //MessageBox.Show("Error opening " + Settings.GPS_ComPort + " at " + Settings.GPS_ComPort_Baudrate + " baudrate. Verify your settings!\nStone will emulate GPS antenna...");
                //moduleGpsOsm.PointTo(new PointF(9.236975f, 45.491090f));  
                GpsHandler.Stop();
                GpsHandler.EnableEmulate(Settings.NMEA_path);
                GpsHandler.Start(Settings.GPS_ComPort, int.Parse(Settings.GPS_ComPort_Baudrate));
            }

            timerPaint.Start();
        }
        private void Log(string text)
        {
            File.AppendAllText(Settings.LOG_Path, DateTime.Now.ToString() + " " + text + "\n\r\n\r");
        }
        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            GpsHandler.Stop();
        }        
        private void addNMEAsentenceInListBox(string NMEAsentence)
        {
            if (listBox_LOG.Items.Count >= 5)
                listBox_LOG.Items.RemoveAt(0);

            listBox_LOG.Items.Add(NMEAsentence);
        }
        private void timerPaint_Tick(object sender, EventArgs e)
        {
            //panelViewport_Paint(this, new PaintEventArgs(panelViewport.CreateGraphics(), new Rectangle()));
            panelViewport.Invalidate();
            GetDebugInformations();
        }

        private void GetDebugInformations()
        {
            listBox_Debug.Items.Clear();

            //RAM
            Process proc = Process.GetCurrentProcess();
            long ramValue = proc.PagedMemorySize64 / 1024;
            listBox_Debug.Items.Add("RAM: " + ramValue + " Kb");

            //RENDER
            listBox_Debug.Items.Add("Render time: " + moduleGpsOsm.LastRenderingTime);

            //LOAD DB
            listBox_Debug.Items.Add("Load data from DB: " + moduleGpsOsm.GetLastLoadDataFromDB());
        }

        private void button_Zoom_IN_Click(object sender, EventArgs e)
        {
            moduleGpsOsm.Zoom_IN();
        }

        private void button_Zoom_OUT_Click(object sender, EventArgs e)
        {
            moduleGpsOsm.Zoom_OUT();
        }
    }
}
